<?php

namespace app\home\home;
use app\admin\controller\Admin;
use app\home\model\User;
use think\Controller;
use think\Db;
use think\Loader;
use think\Request;
use app\common\model\AdminAnnex as AnnexModel;
class Taps extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request)
    {
        //
        $data = $request->param();
        $update['id'] = $data['id'];
        $update['skills'] = $data['skills'];
        $res = User::update($update);
        if ($res){
            return apistatus(1,'更新成功');
        }else{
            return apistatus(0,'更新失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    /**
     * 获取指定人id对应的角色显示技能列表

     */
    public  function getUserTaps(Request $request)
    {
        $data = $request->param();
        if (empty($data['id'])){
            return apistatus(0,'缺少参数');
        }
        $skilist = \db('home_tapcost')->alias("C")->join('home_taps T','C.tapid = T.id')
            ->where(['C.uid'=>$data['id'],'C.status'=>1,'T.type'=>$data['role']])
            ->field('C.id tapcostid,T.name,C.cost,T.price')
            ->select();
        if (!empty($skilist)){
            return apistatus(1,'列表获取成功',$skilist);
        }else{
            return apistatus(0,'该分类下无数据');
        }

    }


    /**
     * 获取指定人id对应的角色显示技能列表

     */
    public  function getRoleTaps(Request $request)
    {
        $data = $request->param();
        if (empty($data['role'])){
            return apistatus(0,'缺少身份参数');
        }
        $where['status'] = 1;
        $where['type'] = $data['role'];
      $list = \db('home_taps')->where($where)->field('id,name')->select();
        if (!empty($list)){
            return apistatus(1,'列表获取成功',$list);
        }else{
            return apistatus(0,'该分类下无数据');
        }

    }



    /**
     * 编辑指定标签价格

     */
    public  function updateTapcost(Request $request)
    {
        $data = $request->param();
        if (empty($data['id']) || empty($data['tapcostid'])){
            return apistatus(0,'缺少参数');
        }
        self::checktoken($data['id']);
        $update['cost'] = $data['cost'];
        $price = \db('home_tapcost')
            ->alias('C')
            ->join('home_taps T','C.tapid=T.id')
            ->where(['C.id'=>$data['tapcostid']])
            ->field('T.price')->find();
        $range = explode('-',$price['price']);
        if (empty($range)){
            return apistatus(0,'该标签已经失效请重新选择');
        }
        if ($update['cost'] > $range[1] || $update['cost']<$range[0]){
            return apistatus(0,'超过范围请重试');
        }
        $res = \db('home_tapcost')->where('id',$data['tapcostid'])->update($update);
        if (!empty($res)){
            return apistatus(1,'编辑成功');
        }else{
            return apistatus(0,'操作失败请重试');
        }

    }



}
